from django.db import models
from utils import utils


# Create your models here.

# USER表
class User(models.Model):
    # id UUID生成唯一ID
    user_id = models.CharField(max_length=128, primary_key=True, db_index=True)
    # open_id
    open_id = models.CharField(max_length=64, unique=True)
    # 昵称
    nickname = models.CharField(max_length=256)
    # 头像url
    image_url = models.CharField(max_length=256)
    # 省份
    province = models.CharField(max_length=32)
    # 城市
    city = models.CharField(max_length=32)
    # 性别 'max_length' is ignored when used with IntegerField.
    sex = models.IntegerField(default=0)
    # 收件数
    receive = models.IntegerField()
    # 发件数
    post = models.IntegerField()
    # 生日
    birthday = models.DateField()
    # 当天是否已经登录过
    hasEntered = models.BooleanField(default=False)


# Message表:
class Message(models.Model):
    # 发件人
    from_id = models.CharField(max_length=32)
    # 收件人
    to_id = models.CharField(max_length=32)
    # 内容
    text = models.TextField(max_length=128)
    # 发送的时间
    post_date = models.DateTimeField()
    # 延迟时间
    during_time = models.DateTimeField()
    # 邮票id
    stamp_id = models.CharField(max_length=32)
    # 目的省份
    province = models.CharField(max_length=32)
    # 目的城市
    city = models.CharField(max_length=32)


# stamp
class Stamp(models.Model):
    # 默认的有邮票id

    # 邮票地址
    url = models.CharField(max_length=64)
    # 邮票名字
    name = models.CharField(max_length=32)


class Match(models.Model):
    id = models.AutoField(primary_key=True)
    # 用户
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    # 年龄范围
    max_age = models.IntegerField()
    min_age = models.IntegerField()
    # 是否愿意匹配
    auto_match = models.BooleanField(max_length=1)


# Interest
class Interest(models.Model):
    # 兴趣id
    id = models.AutoField(primary_key=True)
    # 兴趣name
    name = models.CharField(max_length=10)


class UserInterest(models.Model):
    # 级联删除 与User表相联系
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    # 兴趣 与interest相关系
    interest = models.ForeignKey(Interest, on_delete=models.CASCADE)


class Language(models.Model):
    # 语言名字
    name = models.CharField(max_length=20)


class UserLanguage(models.Model):
    # 语言id
    # id = models.AutoField(primary_key=True)
    # 用户
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    # 语言
    language = models.ForeignKey(Language, on_delete=models.CASCADE)
    # 熟练度 1 2 3 没有掌握就是0
    proficiency = models.IntegerField()


class UserStamp(models.Model):
    mount = models.IntegerField(default=0)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    stamp = models.ForeignKey(Stamp, on_delete=models.CASCADE)


class Letter(models.Model):
    letter_id = models.AutoField(primary_key=True)

    # post_user 和 receive_user 都是 User 的主键
    # 此时两者的反向关联名都为User.set()，此时会产生冲突
    # 所以要设置 related_name 来进行区别
    # https://stackoverflow.com/questions/13918968/multiple-many-to-many-relations-to-the-same-model-in-django/13919198?newreg=18c0b3c7e1ee4bb6ad4f6be9a3027a2f
    post_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='post_user')
    receive_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='receive_user')

    stamp = models.ForeignKey(Stamp, on_delete=models.SET_NULL, null=True)
    text = models.TextField()
    post_time = models.DateTimeField(auto_now_add=True)
    delay_hour = models.IntegerField()
    # upload_to 修改了图片名称
    picture = models.ImageField(null=True, upload_to=utils.user_avatar_path)
    receive_time = models.DateTimeField(null=True)

# 共同兴趣
# class UserUserInterests(models.Model):
#     user1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_id')
#     user2 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_id')
#     commeninterest = models.CharField(max_length=64)
#     commenlanguage = models.CharField(max_length=64)
